package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/update")
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//       0，乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//1.取值页面的值
        String idString = req.getParameter("id");
        int id = 0;
        try {
            id = Integer.parseInt(idString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String oldPassword = req.getParameter("oldPassword");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String hobby = req.getParameter("hobby");
        hobby = hobby.replace("[", "").replace("]", "");
        hobby = hobby.replace("\"", "");
        String introduce = req.getParameter("introduce");

//2.封装
        UserModel userModel = new UserModel();
        userModel.setId(id);
        userModel.setUsername(username);
        userModel.setPassword(password);
        userModel.setSex(sex);
        userModel.setPhone(phone);
        userModel.setBirthday(birthday);
        userModel.setHobby(hobby);
        userModel.setIntroduce(introduce);
        userModel.setOldPassword(oldPassword);
//        3.调用服务逻辑层
        IUserService iUserService = new UserServiceImpl();
//        4.得到返回结果
        int result = iUserService.update(userModel);

//        5.返回页面数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);

    }
}
